<template>
  <d2-container>
    <el-form :model="FormSearch" status-icon label-width="100px" class="demo-ruleForm">
      <!-- <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item label="条件查找" prop="name">
            <el-input type="text" v-model="FormSearch.name" placeholder="请输入名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="searchClick()">查询</el-button>
          <el-button @click="resetForm()">重置</el-button>
        </el-col>
      </el-row>-->
    </el-form>
    <!-- 数据表格 -->
    <d2-crud
      ref="d2Crud"
      :columns="columns"
      :data="data"
      :rowHandle="rowHandle"
      :edit-template="editTemplate"
      :form-options="formOptions"
      :pagination="page"
      @row-edit="handleRowEdit"
      @dialog-cancel="handleDialogCancel"
      @pagination-current-change="paginationCurrentChange"
    ></d2-crud>
  </d2-container>
</template>

<script>
import {
  getXntAccountOrMessList,
  setXntManufactor,
  setXntWholesaler
} from "@/api/accountAdmin/index";

export default {
  data() {
    return {
      // 搜索数据
      FormSearch: {
        name: ""
      },
      // 字段名称
      columns: [
        {
          title: "名称",
          key: "name",
          align: "center"
        },
        {
          title: "电话",
          key: "phone",
          width: 120,
          align: "center"
        },
        {
          title: "地址",
          key: "address",
          align: "center"
        },
        {
          title: "土地",
          key: "acres",
          align: "center"
        },
        {
          title: "龙易付商户代码",
          key: "merchantid",
          width: 200,
          align: "center"
        },
        {
          title: "分行代码",
          key: "branchid",
          align: "center"
        },
        {
          title: "柜台代码",
          key: "posid",
          align: "center"
        },
        {
          title: "支付密钥",
          key: "keyccb",
          align: "center"
        },
        {
          title: "商户服务平台登录密码",
          key: "qrpwd",
          width: 200,
          align: "center"
        }
      ],
      // 列表数据
      data: [],
      // 操作列数据
      rowHandle: {
        columnHeader: "编辑信息",
        align: "center",
        edit: {
          icon: "el-icon-edit",
          text: "编辑信息",
          size: "small"
        }
      },
      editTemplate: {
        name: {
          title: "名称",
          value: ""
        },
        phone: {
          title: "电话",
          value: ""
        },
        address: {
          title: "地址",
          value: ""
        },
        acres: {
          title: "土地",
          value: ""
        },
        merchantid: {
          title: "龙易付商户代码",
          value: ""
        },
        branchid: {
          title: "分行代码",
          value: ""
        },
        posid: {
          title: "柜台代码",
          value: ""
        },
        keyccb: {
          title: "支付密钥",
          value: ""
        },
        qrpwd: {
          title: "商户服务平台登录密码",
          value: ""
        }
      },
      formOptions: {
        labelWidth: "80px",
        labelPosition: "left",
        saveLoading: false
      },
      // 分页数据
      page: {
        total: 0,
        pageIndex: 1,
        pageSize: 10
      }
    };
  },
  created() {
    console.log(this.$route.query);
    let params = {
      page: this.page.pageIndex,
      limit: this.page.pageSize,
      account: this.$route.query.account,
      type: this.$route.query.typecode
    };
    this.init(params);
  },
  methods: {
    // 初始化身体数据列表
    init(params) {
      getXntAccountOrMessList(params)
        .then(res => {
          if (res.success) {
            this.data = res.rows;
            this.page.total = res.total;
          }
        })
        .catch(err => {
          this.$message.error(err);
        });
    },

    // 分页数改变，调用
    paginationCurrentChange(pageIndex) {
      let params = {
        limit: this.page.pageSize,
        name: this.FormSearch.name
      };
      params.page = this.page.pageIndex = pageIndex;
      this.init(params);
    },

    // 修改信息
    handleRowEdit({ index, row }, done) {
      this.formOptions.saveLoading = true;
      let params = {};
      params.id = row.role_id;
      params.name = row.name;
      params.phone = row.phone;
      params.address = row.address;
      params.merchantid = row.merchantid;
      params.branchid = row.branchid;
      params.posid = row.posid;
      params.keyccb = row.keyccb;
      params.qrpwd = row.qrpwd;
      if (row.typecode === "0") {
        setXntManufactor(params)
          .then(res => {
            if (res.success) {
              this.$message({
                message: "编辑成功",
                type: "success"
              });
            }

            // done可以传入一个对象来修改提交的某个字段
            done();
            // 保存按钮的loading状态
            this.formOptions.saveLoading = false;
            // 重新加载列表
            let params = {
              page: this.page.pageIndex,
              limit: this.page.pageSize,
              account: this.$route.query.account,
              type: this.$route.query.typecode
            };
            this.init(params);
          })
          .catch(err => {
            this.$message.error(err);
          });
      } else if (row.typecode === "1") {
        setXntWholesaler(params)
          .then(res => {
            if (res.success) {
              this.$message({
                message: "编辑成功",
                type: "success"
              });
            }

            // done可以传入一个对象来修改提交的某个字段
            done();
            // 保存按钮的loading状态
            this.formOptions.saveLoading = false;
            // 重新加载列表
            let params = {
              page: this.page.pageIndex,
              limit: this.page.pageSize,
              account: this.$route.query.account,
              type: this.$route.query.typecode
            };
            this.init(params);
          })
          .catch(err => {
            this.$message.error(err);
          });
      }
    },

    // 取消修改信息
    handleDialogCancel(done) {
      done();
    },

    // 条件查询搜索
    searchClick() {
      let params = {
        page: 1
      };
      params.name = this.FormSearch.name;
      this.init(params);
    },

    // 重置查询
    resetForm() {
      this.FormSearch.name = "";
      let params = {
        page: 1,
        limit: 10
      };
      this.init(params);
    }
  }
};
</script>
<style lang="scss" scoped>
</style>
